import 'package:flutter/material.dart';
// 路由相关

class MyRoute extends StatefulWidget {
  @override
  _Route createState() => new _Route();
}

class _Route extends State<MyRoute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("路由相关"),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            FlatButton(
              child: Text("全屏Dialog"),
              textColor: Colors.green,
              onPressed: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) {
                          return _FullDialog();
                        },
                        maintainState: false,
                        fullscreenDialog: true));
              },
            ),
            RaisedButton(
              child: Text("返回带参数(Navigator.push)"),
              onPressed: () async {
                var result = await Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return _TipRoute(text: "我是提示xxx");
                }));
                print("路由返回值：$result");
              },
            ),
            RaisedButton(
              child: Text("Context测试(Navigator.of方式)"),
              onPressed: () {
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (context) {
                  return _ContextRoute();
                }));
              },
            ),
            _Echo(
              text: "测试组件传参数11",
              backgroundColor: Colors.blueAccent,
            )
          ],
        ),
      ),
    );
  }
}

///  返回带参数
class _TipRoute extends StatelessWidget {
  final String text;

  _TipRoute({Key key, @required this.text}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("提示"),
      ),
      body: Padding(
        padding: EdgeInsets.all(16),
        child: Center(
          child: Column(
            children: <Widget>[
              Text(text),
              RaisedButton(
                onPressed: () => Navigator.pop(context, '我是返回值'),
                child: Text("返回"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

/// 组件传参数
class _Echo extends StatelessWidget {
  const _Echo({
    Key key,
    @required this.text,
    this.backgroundColor: Colors.green,
  }) : super(key: key);
  final String text;
  final Color backgroundColor;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: backgroundColor,
        child: Text(text),
      ),
    );
  }
}

/// context
class _ContextRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Context测试"),
      ),
      body: Container(
        child: Builder(builder: (context) {
          //  在Widget树中向上查找最近的父级`Scaffold` widget
          Scaffold scaffold = context.findAncestorWidgetOfExactType<Scaffold>();
          // 直接返回 AppBar的title， 此处实际上是Text("Context测试")
          return (scaffold.appBar as AppBar).title;
        }),
      ),
    );
  }
}

///全屏dialog
class _FullDialog extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("全屏dialog测试"),
      ),
      body: Center(
        child: Text("dialog acontent"),
      ),
    );
  }
}
